package com.evil.kingdom.service.config;

import com.alibaba.fastjson.JSON;
import com.evil.kingdom.pojo.entity.config.Config;
import com.evil.kingdom.pojo.entity.config.ConfigValue;
import com.evil.kingdom.pojo.enums.ConfigEnum;
import com.evil.common.mongo.service.impl.SingleServiceImpl;
import com.evil.kingdom.repository.config.ConfigRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 系统配置
 *
 * @author liyang
 * @date 2024-05-22 17:46
 */
@Slf4j
@Service
public class ConfigServiceImpl extends SingleServiceImpl<ConfigRepository, Config> implements ConfigService {

    @Override
    public Optional<ConfigValue> isExistByEnum(ConfigEnum configEnum) {
        return this.isExistByName(configEnum.name())
                .map(config -> JSON.parseObject(config.getValue(), configEnum.getValueClass()));
    }

    @Override
    public ConfigValue findByEnum(ConfigEnum configEnum) {
        return this.isExistByEnum(configEnum).orElseThrow(ConfigEnum::error);
    }
}
